
module DrunkenMaster
  class HtmlApplication

    attr_accessor :router

    def initialize
      self.router = Routing::Router.new
    end

    def execute(request, response)
      handler, tail = router.handler_and_tail(request[:rack_env]['PATH_INFO'])
      unless handler.is_a? FileHandler
        Loader::Load.reload
      end
      c = context(request, response)
      c.tail = tail
      handler.execute(c)
    end
    
    def self.context_for_test      
      self.new.send(:context, {:rack_env => {'PATH_INFO' => '/'}}, HttpResponse.new)
    end    
    
  private  
    
    def context(request, response)
      c = Context.new
      c.response = response
      c.application = self
      c.rack_env = request[:rack_env]
      c.request = Rack::Request.new(c.rack_env)
      c.page = HtmlPage.new()
      c      
    end

  end  
end